/**
 * jims
 */
package com.jims.master.modules.base.inquiry.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.modules.base.inquiry.dao.InquiryAskDao;
import com.jims.master.modules.base.inquiry.entity.InquiryAsk;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import com.jims.master.modules.sys.utils.netease.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 问诊Service
 *
 * @author chent
 * @version 2017-05-12
 */
@Service
@Transactional(readOnly = true)
public class InquiryAskService extends CrudService<InquiryAskDao, InquiryAsk> {

    /**
     * 个人中心我的问诊记录
     * @param page
     * @param inquiryAsk
     * @author chent
     * @return
     */
    public Page<InquiryAsk> findMyInquiryPage(Page<InquiryAsk> page, InquiryAsk inquiryAsk) {
        inquiryAsk.setPage(page);
        page.setPageSize(6);
        page.setList(dao.getInquiryAskListbyPerson(inquiryAsk));
        return page;
    }

    public Page<InquiryAsk> getId(Page<InquiryAsk> page, InquiryAsk inquiryAsk){
        inquiryAsk.setPage(page);
        page.setList(dao.getId(inquiryAsk));
        return page;
    }

    /**
     * 医生端问诊记录
     * @return
     * @author chent
     */
    public Page<InquiryAsk> doctorNotInquiryAsk(Page<InquiryAsk> page, InquiryAsk inquiryAsk) {
        inquiryAsk.setPage(page);
        page.setPageSize(6);
        page.setList(dao.doctorNotInquiryAsk(inquiryAsk));
        return page;
    }

    /**
     * 医生未读数量
     * @param id
     * @return
     */
    public String notReadNum(String id){
        return dao.notReadNum(id);
    }

    /**
     * 医生的问诊列表
     * @param doctor
     * @return
     * @author chent
     */
    public List<InquiryAsk> getInquiryAskList(String doctor, String conntFlag, int startPage) {
        return dao.getInquiryAskList(doctor, conntFlag, startPage);
    }

    /*@Transactional(readOnly = false)
    public int updateDoctor(InquiryAsk inquiryAsk) {
        return dao.updateDoctor(inquiryAsk);
    }*/

    /**
     * 病人咨询详情
     *
     * @param id
     * @return
     * @author chent
     */
    public List<InquiryAsk> personAllAsk(String id, String conntFlag, int startPage) {
        return dao.personAllAsk(id, conntFlag, startPage);
    }

    /**
     * 用户咨询历史记录
     *
     * @param personId
     * @return
     * @author chent
     */
    public List<InquiryAsk> inquiryListByUser(String personId) {
        return dao.inquiryListByUser(personId);
    }

    /**
     * 医生端问诊历史记录
     * @return
     * @author chent
     */
    public List<InquiryAsk> doctorInquiryAsk(InquiryAsk inquiryAsk) {
        return dao.doctorInquiryAsk(inquiryAsk);
    }

    /**
     * 在线咨询没有问诊医生
     * @return
     * @author chent
     */
    public List<InquiryAsk> allInquiryAsk() {
        return dao.allInquiryAsk();
    }

    /**
     * 在线问诊上传图片
     * @param file
     * @param request
     * @return
     * @author jicunteng
     * @throws Exception
     */
    @Transactional(readOnly = false)
    public String savePics(MultipartFile file, HttpServletRequest request) throws Exception {
        User user = UserUtils.getUser();
        String home = request.getSession().getServletContext().getRealPath("");
        String pathImg = "inquiryAskImage";
        String datePath = DateUtils.formatDate(new Date(), "yyyy/MM/dd");
        String pathUrl = File.separator + pathImg + File.separator + datePath + File.separator + "inquiryAskImage";
        String str = UUIDUtil.getUUID();
        String headPath = str+"."+file.getOriginalFilename()+ ".";
        String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length());
        pathUrl = pathUrl + File.separator + headPath + fileName;
        File fileTemp = new File(home + pathUrl);
        if (!fileTemp.exists())
            fileTemp.mkdirs();

        file.transferTo(fileTemp);
        //文件名为当前时间
        return pathUrl;
    }

    /**
     * 医生端问诊量
     * @return
     * @author chent
     */
    public String getCountByDocId(String personId) {
        return dao.getCountByDocId(personId);
    }

    /**
     * 检查医生是否已经领取该问题
     * @param acceptAskId
     * @param doctor
     * @return
     * @author chent
     */
    public InquiryAsk getAcceptId(String acceptAskId, String doctor) {
        return dao.getAcceptId(acceptAskId, doctor);
    }

    /**
     * 医生问诊记录详情
     * @return
     * @author chent
     */
    public List<InquiryAsk> doctorInquiryDetail(InquiryAsk inquiryAsk) {
        //详情集合
        List<InquiryAsk> doctorInquiryDetail = dao.doctorInquiryDetail(inquiryAsk);
        //患者问诊图片分割并放进集合
        for(int i = 0; i < doctorInquiryDetail.size(); i++){
            List askPathList = new ArrayList();
            String askPath = doctorInquiryDetail.get(i).getAskPath();
            if(askPath!=null&&askPath!=""){
                String a[] = askPath.split(",");
                for (int j = 0; j < a.length; j++){
                    askPathList.add(a[j]);
                }
            }
            doctorInquiryDetail.get(i).setAskPathList(askPathList);
        }
        return dao.doctorInquiryDetail(inquiryAsk);
    }

    /**
     * 修改医生已读状态
     * @param inquiryAsk
     * @return
     */
    @Transactional(readOnly = false)
    public int updateReadState(InquiryAsk inquiryAsk) {
        return dao.updateReadState(inquiryAsk);
    }

}